﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Text;

namespace SqlSystemWatcher.Common
{
    public class CustomException
    {
        public enum InfoScreenLevel
        {
            Information,
            Exception,
            SuccessfullyCompleted
        }

        private const string SourceClassName = "CustomException";

        public static bool WriteInfoScreen(InfoScreenLevel infoScreenLevel, string sourceClassName, string programCode,
                                           string connectionString, string instanceName, string commandText, Exception exception,
                                           string detailMessage)
        {
            var strbInformationContent = new StringBuilder();

            strbInformationContent.Append("Timestamp:=" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
            strbInformationContent.Append("|");
            strbInformationContent.Append("SourceClassName:=" + sourceClassName);
            strbInformationContent.Append("|");
            strbInformationContent.Append("ProgramCode:=" + programCode);
            strbInformationContent.Append("|");
            strbInformationContent.Append("ConnectionString:=" + connectionString);
            strbInformationContent.Append("|");
            strbInformationContent.Append("CommandText:=" + commandText);
            strbInformationContent.Append("|");

            if (exception != null)
            {
                strbInformationContent.Append("ExceptionMessage:=" + exception.Message);
            }
            else
            {
                strbInformationContent.Append("DetailMessage:=" + detailMessage);
            }

            const string eventLogSource = "SqlSystemWatcher";
            const string eventLog = "Application";
            string _event = strbInformationContent.ToString();

            if (!EventLog.SourceExists(eventLogSource))
            {
                EventLog.CreateEventSource(eventLogSource, eventLog);
            }

            if (infoScreenLevel == InfoScreenLevel.Information)
                EventLog.WriteEntry(eventLogSource, _event, EventLogEntryType.Information);
            else if (infoScreenLevel == InfoScreenLevel.Exception)
            {
                EventLog.WriteEntry(eventLogSource, _event, EventLogEntryType.Error);
            }

            ConsoleColor currentColor = Console.ForegroundColor;
            try
            {
                switch (infoScreenLevel)
                {
                    case InfoScreenLevel.Information:
                        Console.ForegroundColor = ConsoleColor.Green;
                        break;
                    case InfoScreenLevel.Exception:
                        Console.ForegroundColor = ConsoleColor.Red;

                        if (exception != null)
                        {
                            var errorLoggerCore = new ErrorLoggerCore(instanceName, exception.Message);
                            errorLoggerCore.DoIt();
                        }

                        break;
                }

                Console.WriteLine(strbInformationContent.ToString());

                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                Console.ForegroundColor = currentColor;
            }
        }
    }
}